package com.example.restaurantfinder;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.example.restaurantfinder.config.Configuration;
import com.example.restaurantfinder.model.RestaurantInfo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.util.Base64;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class RestaurantDetailInfo extends Activity {
	
	private static final String HTML_FORMAT = "<html><body style=\"text-align: center; background-color: black; vertical-align: center;\"><img src = \"%s\" /></body></html>";
	private WebView mWebView;
	
	private RestaurantInfo selectedRestaurant ; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.restaurant_details);
		
		Bundle b = getIntent().getExtras();
		if(b.getParcelable("com.example.restaurantfinder.model.RestaurantInfo") != null){
			selectedRestaurant =
	        	b.getParcelable("com.example.restaurantfinder.model.RestaurantInfo");
		}	
		
		String imageURL = selectedRestaurant.getIcon();
		mWebView = (WebView) findViewById(R.id.icon);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.loadUrl(imageURL);
		/*TextView idTV = (TextView)findViewById(R.id.restId);
		//idTV.setText("Id:" + selectedRestaurant.getId());
		idTV.setText(Html.fromHtml("<b>ID:</b>" + selectedRestaurant.getId()));*/
		
		TextView nameTV = (TextView)findViewById(R.id.name);
		//nameTV.setText("Name: " + selectedRestaurant.getName());
		nameTV.setText(Html.fromHtml("<b>Name:</b>" + selectedRestaurant.getName()));
		
		/*TextView referenceTV = (TextView)findViewById(R.id.reference);
		//referenceTV.setText("Reference: " + selectedRestaurant.getReference());
		referenceTV.setText(Html.fromHtml("<b>Reference:</b>" + selectedRestaurant.getReference()));*/
		
		/*TextView iconTV = (TextView)findViewById(R.id.icon);
		//iconTV.setText("Icon: " + selectedRestaurant.getIcon());
		iconTV.setText(Html.fromHtml("<b>Icon:</b>" + selectedRestaurant.getIcon()));*/
		
		/*changes to display image start*/
		
		
		//String imageURL = selectedRestaurant.getIcon();
		
		
		//final String imgUrl = imageURL;
		
		/*mWebView = (WebView) findViewById(R.id.icon);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.loadUrl(imageURL);
	    */
	    
		
		/*changes to display image end*/
		
		TextView vicinityTV = (TextView)findViewById(R.id.vicinity);
		//vicinityTV.setText("Vicinity: " + selectedRestaurant.getVicinity());
		vicinityTV.setText(Html.fromHtml("<b>Vicinity:</b>" + selectedRestaurant.getVicinity()));
		
		TextView ratingTV = (TextView)findViewById(R.id.rating);
		//ratingTV.setText("Rating: " + selectedRestaurant.getRating());
		ratingTV.setText(Html.fromHtml("<b>Rating:</b>" + selectedRestaurant.getRating()));
		
		TextView urlTV = (TextView)findViewById(R.id.url);
		//urlTV.setText("URL: " +  selectedRestaurant.getUrl());
		urlTV.setText(Html.fromHtml("<b>URL:</b>" + selectedRestaurant.getUrl()));
		
		TextView websiteTV = (TextView)findViewById(R.id.website);
		//websiteTV.setText("Website: " + selectedRestaurant.getWebsite());
		websiteTV.setText(Html.fromHtml("<b>Website:</b>" + selectedRestaurant.getWebsite()));
		
		TextView addressTV = (TextView)findViewById(R.id.formattedaddress);
		//addressTV.setText("Address: " + selectedRestaurant.getFormatted_address());
		addressTV.setText(Html.fromHtml("<b>Address:</b>" + selectedRestaurant.getFormatted_address()));
		
		TextView phoneTV = (TextView)findViewById(R.id.formattedphonenumber);
		//phoneTV.setText("Phone: " + selectedRestaurant.getFormatted_phone_number());
		phoneTV.setText(Html.fromHtml("<b>Phone:</b>" + selectedRestaurant.getFormatted_phone_number()));
		
		TextView latTV = (TextView)findViewById(R.id.lat);
		//latTV.setText("Latitude: " + selectedRestaurant.getLat());
		latTV.setText(Html.fromHtml("<b>Latitude:</b>" + selectedRestaurant.getLat()));
		
		TextView longTV = (TextView)findViewById(R.id.lng);
		//longTV.setText("Longitude: " + selectedRestaurant.getLng());
		longTV.setText(Html.fromHtml("<b>Longitude:</b>" + selectedRestaurant.getLng()));
	}
	
	@SuppressLint("NewApi")
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.diritems, menu);
		View v = (View) menu.findItem(R.id.getdir).getActionView();
		Button button = ( Button ) v.findViewById(R.id.dirbtn);
		button.setOnClickListener(new OnClickListener() {
			 
			@Override
			public void onClick(View arg0) {
 
				Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(Configuration.googleMapsLink + "saddr=-33.8670522,151.1957362&daddr="+ selectedRestaurant.getLat() +","+selectedRestaurant.getLng()));
				//Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(Configuration.googleMapsLink + "saddr=" + selectedRestaurant.getCurr_lat()+ "," + selectedRestaurant.getCurr_lng()+ "&daddr=" + selectedRestaurant.getLat() +","+selectedRestaurant.getLng()));
				startActivity(i);
 
			}
 
		});
		
		return super.onCreateOptionsMenu(menu);
		
		//return true;
	}

}
